package com.zhuimeng.second.service.impl;

import com.zhuimeng.second.service.SecondKillService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class SecondKillServiceImpl implements SecondKillService {
    private final String orderCount = "ORDERCOUNT";
    private final String killMessage = "KILLMESSAGE";
    private final String payMessage = "PAYMESSAGE";

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public String secondKill(String userId) {
        if (redisTemplate.hasKey(orderCount) && Integer.parseInt(redisTemplate.boundValueOps(orderCount).get().toString())>0){
            rocketMQTemplate.convertAndSend(killMessage,userId);
            return "抢购成功\t\t\t"+userId;
        }
        return "抢购失败";
    }

    @Override
    public String pay(String userId) {
        rocketMQTemplate.convertAndSend(payMessage,userId);
        return "付款成功";
    }
}
